package com.ohyes.bigbang.demo.springmail.service;

import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import com.ohyes.bigbang.demo.springmail.utils.MailSenderInfo;
import com.ohyes.bigbang.demo.springmail.utils.SimpleMailSender;

@Service
public class SpringMailService implements ISpringMailService {
	@Autowired
	JavaMailSenderImpl mailSender;

	@Override
	public void sendMail(String content) throws Exception {
		// System.getProperties().put( "proxySet", "true" );
		// System.getProperties().put( "proxyHost", "221.130.253.135" );
		// System.getProperties().put( "proxyPort", "8080" );

		System.getProperties().put("socksProxySet", "true");
		System.getProperties().put("socksProxyHost", "221.130.253.135");
		System.getProperties().put("socksProxyPort", "8080");
		System.setProperty("java.net.useSystemProxies", "true");

		MailSenderInfo mailInfo = new MailSenderInfo();
		mailInfo.setMailServerHost("smtp.qq.com");
		mailInfo.setMailServerPort("465");
		mailInfo.setValidate(true);
		mailInfo.setUserName("79381303@qq.com");
		mailInfo.setPassword("123qwe");// 您的邮箱密码
		mailInfo.setFromAddress("79381303@qq.com");
		mailInfo.setToAddress("114507652@qq.com");
		mailInfo.setSubject("设置邮箱标题");
		mailInfo.setContent("设置邮箱内容");
		// 这个类主要来发送邮件
		SimpleMailSender sms = new SimpleMailSender();
		sms.sendTextMail(mailInfo);// 发送文体格式
		sms.sendHtmlMail(mailInfo);// 发送html格式
	}

	private void sendTest() throws Exception {
		MimeMessage message = mailSender.createMimeMessage();
		MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");

		helper.setTo("114507652@qq.com");
		//
		helper.setFrom("78381303");
		// 设置主题
		helper.setSubject("Spring Mail 测试成功！");
		helper.setText("<html><body color='red'>"
				+ "<font size='4' color='red'>屋里哇啦</font>"
				+ "<a href='www.google.com'>谷歌Google</a>" + "</body></html>",
				true);
		mailSender.send(message);
	}

}
